﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class responsableinventarioController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /responsableinventario/

        public ActionResult IndexById(int id)
        {

            var query = entidades.RESPONSABLEINVENTARIOs
                .Where(ri => ri.IdResponsableInventario == id);

            var result = (from ri in query.ToList()
                          join p in entidades.PERSONALs on ri.IdPersonal equals p.IdPersonal
                          select new
                          {
                              IdResponsableInventario = ri.IdResponsableInventario,
                              FechaIni = string.Format("{0:d}", ri.FechaIni),
                              FechaFin = string.Format("{0:d}", ri.FechaFin),
                              ApeNombre = p.ApePaterno.Trim() + " " + p.ApeMaterno.Trim() + " " + p.Nombres.Trim(),
                              IdPersonal = p.IdPersonal,
                              IdInventario = ri.IdInventario
                              //Activo = ri.Activo
                          }).ToList();
            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult IndexByIdInv(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);
            string responsable = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.RESPONSABLEINVENTARIOs
                .Where(ri => ri.IdInventario == id)
                .Join(entidades.PERSONALs, ri => ri.IdPersonal, p => p.IdPersonal, (ri, p) => new {ri.IdResponsableInventario,
                    Responsable = p.ApePaterno.Trim() + " " + p.ApeMaterno.Trim() + " " + p.Nombres.Trim(),
                    ri.FechaIni,ri.FechaFin,ri.Activo
                });

            if (!String.IsNullOrEmpty(responsable))
            {
                query = query.Where(cs => cs.Responsable.ToUpper().Contains(responsable.ToUpper()));
            }

            var result = (from ri in query.OrderByDescending(ri => ri.IdResponsableInventario).Skip(start).Take(limit).ToList()
                          select new
                          {
                              IdResponsableInventario = ri.IdResponsableInventario,
                              FechaIni = string.Format("{0:d}", ri.FechaIni),
                              FechaFin = string.Format("{0:d}", ri.FechaFin),
                              Responsable = ri.Responsable,
                              Activo = ri.Activo
                          }).ToList();
            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /responsableinventario/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /responsableinventario/Create

        //public ActionResult Create()
        //{
        //    return View();
        //} 

        //
        // POST: /responsableinventario/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            string strResponse = String.Empty;
            try
            {
                // TODO: Add insert logic here
                var crespinv = new RESPONSABLEINVENTARIO();
                if (ModelState.IsValid)
                {
                    TryUpdateModel(crespinv, collection.ToValueProvider());
                    entidades.INVInsertResponsable(crespinv.IdInventario, crespinv.IdPersonal, crespinv.FechaIni);
                    strResponse = "{success: true,msg:'Registro Creado Correctamente'}";
                    return Content(strResponse);
                }
                strResponse = "{success: false,msg:'Fallo en la Creación'}";
                return Content(strResponse);
            }
            catch
            {
                strResponse = "{success: false,msg:'Fallo en la Creación'}";
                return Content(strResponse);
            }
        }
        
        //
        // GET: /responsableinventario/Edit/5
 
        //public ActionResult Edit(int id)
        //{
        //    return View();
        //}

        //
        // POST: /responsableinventario/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            string strResponse = String.Empty;
            var eRespInv= entidades.RESPONSABLEINVENTARIOs.Single(p => p.IdResponsableInventario == id);
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    TryUpdateModel(eRespInv);
                    entidades.SaveChanges();
                    strResponse = "{success: true,msg:'Registro Creado Correctamente'}";
                    return Content(strResponse);

                }
                strResponse = "{success: false,msg:'Fallo en la Creación'}";
                return Content(strResponse);
            }
            catch
            {
                strResponse = "{success: false,msg:'Fallo en la Creación'}";
                return Content(strResponse);
            } 
        }

        //
        // GET: /responsableinventario/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /responsableinventario/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
